var ContextMenu = Class.create();

ContextMenu.prototype = //Object.extend( new Element(),
{
  initialize: function()
  {
    var docm = document;
    var element = Element.extend( docm.createElement( 'div' ) );
    docm.body.appendChild( element );
    element.className = 'contextMenu';  
    element.id = 'context_menu'
    this.items = new Array()
    var menu = [
//      item        hover       handler     id
      ['Edit','make area editable',0]   /*  Edit item   */
     ,['Save','save area content',1]    /*  Save item   */
     ,['Quit','quit from edit mode',2]  /*  Quit item   */
     ,['Html/Rich text','switch between html and rich text',4]       /*  Switch html/rich text */
     ,['Cancel','cancel changes',3]     /*  Cancel item */
     ,['Close','close menu',5]          /*  Close item */
    ]
    
    for( var i = 0; i < menu.length; i++ )
    {
      var div = Element.extend( docm.createElement( 'div' ) );
      var a = Element.extend( docm.createElement( 'a' ) );
      a.href = 'javascript:void(0)'
      a.title = menu[i][1];
      a.innerHTML = menu[i][0];
      Event.observe( a, 'click', this.doOnClick.bindAsEventListener( this, menu[i][2] ) );
      div.appendChild( a );
      element.appendChild( div );
//      this.items.push( a );
      this.items[ menu[i][2] ] = a;
    }

    this.menu = element;
    this.target = null;
  },
  
  doOnClick: function( event, i )
  {
//    alert( i + '; L=' + arguments.length + '; arg[0]=' + arguments[0]+ '; arg[1]=' + arguments[1] + '; arg[2]=' + arguments[2] )
    switch( i )
    {
      case 0:
        if( this.target )
        {
          if( this.prev != null && this.prev != this.target )
            this.prev.unsetEditable();
          this.target.setEditable();
        }
        this.hide();
        break;
      case 1:
        if( this.target )
        {
          this.target.save();
        }  
        this.hide();
        break;  
      case 2:
        if( this.target )
        {
          this.target.unsetEditable();
        }
        this.hide();
        break;  
      case 3:
        if( this.target )
        {
          this.target.unsetEditable();
          this.target.load();  // TO DO check dirty flag !!!
        }                      // Attention : no break !!!
      case 5:
        this.hide();
        break;  
      case 4:
        if( this.target )
        {
          if( this.target.htmlMode )
            this.target.switchToRichText();
          else
            this.target.switchToHTML();
        }
        this.hide();
        break;  
      default:
        break;
    }      
  },
  
  show: function( t )
  {
    this.menu.show();
    this.prev = this.target;
    this.target = t;
  },
  
  hide: function()
  {
    this.menu.hide();
//    this.target = null;
  },
  
  setMode: function( m )
  {
    if( ! m )
    { // not editable
      this.items[0].show();
      this.items[1].hide();
      this.items[2].hide();
      this.items[3].hide();
      this.items[4].show();
      this.items[5].show();
    }
    else
    { // editable 
      this.items[0].hide();
      this.items[1].show();
      this.items[2].show();
      this.items[3].show();
      this.items[4].show();
      this.items[5].hide();
    }
  }
}
